<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('repos/usermodel', 'UserModel');
    }

    public function index()
    {
        $this->info();
    }

    public function login()
    {
        $this->session->keep_flashdata('referrer');
        $this->session->unset_userdata('userlogin');
        $this->load->view("common/loginlayout");
    }

    public function dologin()
    {
        $uname = $this->input->post('login', true);
        $upass = md5($this->input->post('password', true));

        $loginResult = $this->UserModel->doLogin($uname, $upass);

        if ($loginResult > 0) {
            $url = base_url($this->session->flashdata('referrer'));
            $newdata = array('userid' => $loginResult, 'logged_in' => true);
            $this->session->set_userdata('userlogin', $newdata);
            redirect($url);
        } else {
            $this->session->set_flashdata('error',
                '<span class="error">Login infomation was incorrect !</span>');
            redirect(base_url(array('user', 'login')));
        }
    }

    public function logout()
    {
        $this->session->unset_userdata('userlogin');
        redirect(base_url(array('user', 'login')));
    }

    public function info()
    {
        $this->load->model('repos/usermodel', 'UserModel');
        $user = $this->session->userdata('userlogin');
        $data['userInfo'] = $this->UserModel->getUserInfo($user['userid']);
        try {
            $user['password'] = $data['userInfo']->getPassword();
            $user['email'] = $data['userInfo']->getEmail();
        }
        catch (exception $ex) {
            redirect(base_url(array('user', 'login')));
        }
        $this->session->set_userdata('userlogin', $user);
        $this->renderView('user/info', $data);
    }

    public function doupdate()
    {
        if (!$this->input->post()) {
            show_404();
        }

        $userlogin = $this->session->userdata('userlogin');
        $newInfo = new UserEntity();
        $password = trim($this->input->post('password', true));
        if ($userlogin['password'] != md5($password)) {
            $this->session->set_flashdata('message',
                "<span class='error'>Current password was incorrect !</span>");
            redirect(base_url(array('user', 'info')));
        }

        $bIsNewPass = trim($this->input->post("chknewpass", true));
        if ($bIsNewPass > 0) {
            $newPass = trim($this->input->post('newpassword', true));
            $renewPass = trim($this->input->post('renewpassword', true));
            if ($newPass == "") {
                $this->session->set_flashdata('message',
                    "<span class='error'>New password was invalid !</span>");
                redirect(base_url(array('user', 'info')));
            }
            if (strlen($newPass) < 6) {
                $this->session->set_flashdata('message',
                    "<span class='error'>New password must longer than 5 characters !</span>");
                redirect(base_url(array('user', 'info')));
            }
            if ($newPass != $renewPass) {
                $this->session->set_flashdata('message',
                    "<span class='error'>Confirm new password miss match !</span>");
                redirect(base_url(array('user', 'info')));
            }
            $newInfo->setPassword(md5($newPass));
        } else {
            unset($newInfo->password);
        }

        $email = trim($this->input->post('email', true));
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $this->session->set_flashdata('message',
                "<span class='error'>Invalid email address !</span>");
            redirect(base_url(array('user', 'info')));
        } else {
            $newInfo->setEmail($email);
        }

        unset($newInfo->id);
        unset($newInfo->username);
        unset($newInfo->last_login);
        unset($newInfo->deleted_at);
        $newInfo->setLastUpdate(new DateTime('now', new DateTimeZone(TIMEZONE)));
        $newInfo->setDeleteFlg(0);

        $result = $this->UserModel->updateUserInfo($userlogin["userid"], $newInfo);
        if ($result) {
            $this->session->set_flashdata('message',
                "<span class='success'>Update successfully!</span>");
            $userlogin['password'] = md5($password);
        } else {
            $this->session->set_flashdata('message',
                "<span class='error'>Update failed!</span>");
        }
        redirect(base_url(array('user', 'info')));
    }

}
?>